package com.xd.core.finance.controller;

import com.github.pagehelper.Page;
import com.xd.common.constant.SysConstant;
import com.xd.common.util.SecurityUtil;
import com.xd.core.api.service.IAdvancePaymentOrderService;
import com.xd.core.api.service.IRepaymentOrderInfoService;
import com.xd.core.business.service.IOutInfoService;
import com.xd.core.business.service.IRepaymentScheduleService;
import com.xd.core.company.service.IElectronicTransferInfoService;
import com.xd.core.company.service.IOfflineTransferInfoService;
import com.xd.core.debt.service.IDebtTransRelateService;
import com.xd.core.finance.dto.*;
import com.xd.core.finance.vo.*;
import com.xd.core.mission.entity.MissionCenterInfo;
import com.xd.core.mission.mapper.MissionCenterInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 财务管理-预存款账户管理
 * </p>
 *
 * @author ZCH
 * @since 2025/03/06/13:41
 */
@Api(tags = "财务管理")
@RestController
@RequestMapping("/finance")
@Slf4j
public class FinanceController {

    @Autowired
    private IOfflineTransferInfoService offlineTransferInfoService;
    @Autowired
    private IElectronicTransferInfoService electronicTransferInfoService;
    @Autowired
    private IDebtTransRelateService iDebtTransRelateService;
    @Autowired
    private IOutInfoService outInfoService;
    @Autowired
    private IRepaymentOrderInfoService repaymentOrderInfoService;
    @Autowired
    private MissionCenterInfoMapper missionCenterInfoMapper;
    @Autowired
    private IRepaymentScheduleService iRepaymentScheduleService;
    @Autowired
    private IAdvancePaymentOrderService advancePaymentOrderService;


    @PreAuthorize("hasAnyAuthority('sys:DebtAccount')")
    @PostMapping("/transactionRecord")
    @ApiOperation("充值提现记录-通道一")
    public Page<PreDepositeQueryVo> getPreDepositRecordChannelOne(@RequestBody PreDepositeQueryDto preDepositeQueryDto) {
        return offlineTransferInfoService.getPreDepositRecord(preDepositeQueryDto);
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount')")
    @PostMapping("/paymentRecords")
    @ApiOperation("支付记录-通道一")
    public Page<PaymentRecordQueryVo> getPaymentRecords(@RequestBody PaymentRecordQueryDto paymentRecordQueryDto) {
        return electronicTransferInfoService.getPaymentRecords(paymentRecordQueryDto);
    }

    @PreAuthorize("hasAnyAuthority('sys:BusinessAccount-viewDetails')")
    @PostMapping("/rechargeAndRefundRecords")
    @ApiOperation("退款记录-通道一")
    public Page<RechargeAndRefundQueryVo> getRechargeAndRefundRecords(@RequestBody RechargeAndRefundQueryDto dto) {
        return electronicTransferInfoService.getRechargeAndRefundRecords(dto);
    }

    @PostMapping("/platformRecords")
    @ApiOperation("平台提现记录-官方通道（标准）")
    public Page<PlatformRecordsVo> getPlatformRecords(@RequestBody PlatformRecordsDto dto) {
        return offlineTransferInfoService.getPlatformRecords(dto);
    }

    @PreAuthorize("hasAnyAuthority('sys:FinancialManagements','sys:DebtAccount')")
    @PostMapping("/transactionRecordChannelTwo")
    @ApiOperation("充值提现记录-官方通道（标准）")
    public Page<PreDepositeQueryChannelTwoVo> getPreDepositRecordChannelTwo(@RequestBody PreDepositeQueryChannelTwoDto preDepositeQueryDto) {
        return offlineTransferInfoService.getPreDepositRecordChannelTwo(preDepositeQueryDto);
    }

//    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-viewDetails')")
    @PostMapping("/repayManage")
    @ApiOperation("回款记录（两个渠道共用）")
    public Page<RepayManageVo> getRepayManage(@RequestBody RepayManageDto dto) {
        return repaymentOrderInfoService.getRepayManage(dto);
    }

//    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-viewDetails')")
    @PostMapping("/advancePaymentRecord")
    @ApiOperation("预付款记录(两个渠道共用)")
    public Page<AdvancePaymentRecordVo> getAdvancePaymentRecord(@RequestBody AdvancePaymentRecordDto dto) {
        return advancePaymentOrderService.getAdvancePaymentRecord(dto);
    }

    @PreAuthorize("hasAnyAuthority('sys:BusinessAccount-viewDetails')")
    @PostMapping("/lendingRecords")
    @ApiOperation("放款记录(两个渠道共用)")
    public Page<LendingRecordQueryVo> getLendingRecords(@RequestBody LendingRecordQueryDto dto) {
        return outInfoService.getLendingRecords(dto);
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount')")
    @GetMapping("/debtOrderRelate/{orderNo}")
    @ApiOperation("债权方账户-查询债权和流水关系")
    public List<DebtOrderQueryVo> getDebtOrderRelate(@PathVariable String orderNo) {
        return iDebtTransRelateService.getDebtOrderRelate(orderNo);
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Export')")
    @ApiOperation("通道一充值提现记录导出")
    @PostMapping("/exportPreDepositRecord")
    public void exportPreDepositRecord(@RequestBody PreDepositeQueryDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        offlineTransferInfoService.exportPreDepositRecord(dto, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Export')")
    @ApiOperation("官方通道（标准）充值提现记录导出")
    @PostMapping("/exportPreDepositRecordChannelTwo")
    public void exportPreDepositRecordChannelTwo(@RequestBody PreDepositeQueryChannelTwoDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        offlineTransferInfoService.exportPreDepositRecordChannelTwo(dto, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Export')")
    @ApiOperation("通道一支付记录导出")
    @PostMapping("/exportPaymentRecords")
    public void exportPaymentRecords(@RequestBody PaymentRecordQueryDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        electronicTransferInfoService.exportPaymentRecords(dto, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Export')")
    @ApiOperation("回款记录导出（通道一二共用）")
    @PostMapping("/exportRepayManage")
    public void exportRepayManage(@RequestBody RepayManageDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        repaymentOrderInfoService.exportRepayManage(dto, missionCenterInfo.getId());
    }

    @ApiOperation("预付款记录导出（通道一二共用）")
    @PostMapping("/exportAdvancePaymentRecord")
    public void exportAdvancePaymentRecord(@RequestBody AdvancePaymentRecordDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        advancePaymentOrderService.exportAdvancePaymentRecord(dto, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:BusinessAccount-Export')")
    @ApiOperation("退款记录导出（通道一）")
    @PostMapping("/exportRechargeAndRefundRecords")
    public void exportRechargeAndRefundRecords(@RequestBody RechargeAndRefundQueryDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        electronicTransferInfoService.exportRechargeAndRefundRecords(dto, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:BusinessAccount-Export')")
    @ApiOperation("放款记录导出（通道一二共用）")
    @PostMapping("/exportLendingRecords")
    public void exportLendingRecords(@RequestBody LendingRecordQueryDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        outInfoService.exportLendingRecords(dto, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:DebtAccount-Export')")
    @ApiOperation("平台提现记录导出")
    @PostMapping("/exportPlatformRecords")
    public void exportPlatformRecords(@RequestBody PlatformRecordsDto dto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        offlineTransferInfoService.exportPlatformRecords(dto, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:AccountsReceivableList-RepaymentPosting','sys:RepaymentManagement-RepaymentPosting'," +
            "'sys:Lending-RepaymentAccountings','sys:Lending-RepaymentAccounting','sys:GuaranteeDetail-ListRepaymentAccounting')")
    @ApiOperation("还款记账")
    @PostMapping("/bookkeeping")
    public void bookkeeping(@RequestBody KeepBookingDto keepBookingDto){
        iRepaymentScheduleService.updateBook(keepBookingDto);
    }
}
